package com.work.farm.controller;

import com.alibaba.fastjson.JSON;
import com.work.device.feign.DeviceInfoFeign;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import com.work.farm.dao.FarmLandDeviceDao;
import com.work.farm.pojo.TCustFarmlandDeviceEntity;
import com.work.farm.service.FarmLandDeviceService;
import javafx.scene.media.AudioClip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/farmDevice")
public class FarmLandDeviceController {

    @Autowired
    private FarmLandDeviceService farmLandDeviceService;


    //安装设备
    @PostMapping("/addFarmLandDevice")
    public Result addFarmLandDevice(@RequestBody TCustFarmlandDeviceEntity tCustFarmlandDeviceEntity) {
        try {
            String[] pos = tCustFarmlandDeviceEntity.getPos();
            String s = JSON.toJSONString(pos);
            tCustFarmlandDeviceEntity.setPosition(s);
            farmLandDeviceService.addFarmLandDevice(tCustFarmlandDeviceEntity);
            return new Result(true, StatusCode.ADD_OK, "安装设备成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ADD_ERROR, "安装失败");
        }

    }

    //根据id手动关闭设备
    @PutMapping("/closeDevice")
    public Result closeDevice(Integer id) {
        try {
            farmLandDeviceService.closeStatus(id);
            return new Result(true, StatusCode.CLOSE_OK, "关闭成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.CLOSE_ERROR, "关闭失败");
        }
    }

    //根据手动开启设备
    @PutMapping("/openDevice")
    public Result openDevice(Integer id) {
        try {
            farmLandDeviceService.openStatus(id);
            return new Result(true, StatusCode.OPEN_OK, "开启成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.OPEN_ERROR, "开启失败");
        }
    }

    //自动起停设备
    @PutMapping("/autoOpenAndCloseDevice")
    public Result AutoOpenAndCloseDevice(Integer id, Integer status, String stage) {
        try {
            Integer integer = farmLandDeviceService.autoOpenAndCloseDevice(id, status, stage);
            if (integer==30010){
                return new Result(true, StatusCode.CLOSE_OK,"自动关闭成功");
            }else if (integer == 30020){
                return new Result(true, StatusCode.OPEN_OK,"自动开启成功");
            }else{
                return new Result(false,StatusCode.AUTODEVICE,"请联系管理员");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.AUTODEVICE,"请联系管理员");
        }



    }

}
